var app = getApp();

Page({
    data: {
        is_default: '/images/q.jpg',
        indexs: '',
        name: '',
        tel: '',
        org: '',
        floor: '',
        remarks: '',
        ids: '',
        xx: ''
    },

    onLoad: function(options) {
        var that = this;
        var name = options.name;
        var tel = options.tel;
        var org = options.org;
        var floor = options.floor;
        var remarks = options.remarks;
        var ids = options.id;
        console.log(ids)

        that.setData({
            name: name,
            tel: tel,
            org: org,
            floor: floor,
            remarks: remarks,
            ids: ids
        })

        var schoolId = app.globalData.schoolId;
        wx.request({
            url: app.globalData.url+ 'getFloorListByOrgId.action?orgId=' + schoolId,
            success: function(res) {
                var array = [];
                var id = [];
                var iddata = res.data[0];
                res.data[0].forEach(function(data, index) {
                    array.push(data.name);
                    id.push(data.id);
                });
                array.unshift('请选择楼号')
                console.log(id)
                that.setData({
                    array: array,
                    id: id,
                    iddata: iddata
                })
            }
        })
    },

    // onShow:function(e){
    //   var that = this;
    //   var name = that.data.name;
    //   var tel = that.data.tel;
    //   var org = that.data.org;
    //   var floor = that.data.floor;
    //   var remarks = that.data.remarks;
    //   var ids = that.data.id;

    //   that.setData({
    //     name: name,
    //     tel: tel,
    //     org: org,
    //     floor: floor,
    //     remarks: remarks,
    //     ids: ids
    //   })
    // },

    //姓名
    onName: function(e) {
        var name = e.detail.value;
        if (this.validate(name)) {
            this.setData({
                name: ''
            })
            return false
        }

        this.setData({
            name: name
        })
    },

    //电话
    onTel: function(e) {
        var tel = e.detail.value;
        if (this.validate(tel)) {
            this.setData({
                tel: ''
            })
            return false
        }
        this.setData({
            tel: tel
        })
    },
//  楼号
    bindPickerChange: function(e) {
      var o = e.currentTarget.dataset
        console.log(o);
        var id = this.data.id;
        if (this.validate(id)) return false

        this.setData({
            index: e.detail.value,
            indexs: id[e.detail.value - 1],
            listindex: e.detail.value,
        })

    },


    //详细地址
    onXx: function(e) {
        var xx = e.detail.value;
        if (this.validate(xx)) {
            this.setData({
                remarks: ''
            })
            return false
        }
        this.setData({
            remarks: xx
        })
    },

    //设为默认
    select1: function(e) {
        if (this.data.is_default == '/images/q.jpg') {
            this.setData({
                is_default: '/images/d.jpg',
                default_code: 1
            })
        } else if (this.data.is_default == '/images/d.jpg') {
            this.setData({
                is_default: '/images/q.jpg',
                default_code: 0
            })
        }
    },
    validate: function(str) {
        if (str == null) return true;
        if (typeof(str) == 'undefined') return true
        if (typeof str == 'string') {
            if (str == "") return true;
            let reg = new RegExp("^([ ]+)|([　]+)$")
            return reg.test(str)
        }
        return false
    },

    onSubmit: function(e) {

        if (this.data.is_default == '/images/q.jpg') {
            this.setData({
                default_code: 0
            })
        } else if (this.data.is_default == '/images/d.jpg') {
            this.setData({
                default_code: 1
            })
        }

        var schoolId = app.globalData.schoolId;
        var openId = app.globalData.openId;
        var name = this.data.name;
        var tel = this.data.tel;
        // var num = this.data.num;
        var remarks = this.data.remarks;
        var default_code = this.data.default_code;
        var index = this.data.indexs;
        var ids = this.data.ids;
        if (!schoolId || !openId || !name || !tel ||
            !remarks || !index.length) {
            wx.showToast({
                title: '请完善信息',
            })
        } else {
            wx.request({
                url: app.globalData.url+ 'updateAddress.action?orgId=' + schoolId + '&openId=' +
                    openId + '&name=' + name + '&telephone=' + tel + '&floorNum=' +
                    index + '&remarks=' + remarks + '&ifMr=' + default_code + '&id=' + ids,

                success: function(res) {
                    console.log(res.data);
                    if (res.data == 'ajaxSuccess') {
                        wx.showToast({
                            title: '修改成功',
                        })

                        wx.redirectTo({
                            url: '/pages/address/list/list',
                        })
                    }
                }
            })
        }
    }

})